package com.lh.aspject;

import com.alibaba.fastjson2.JSON;
import com.lh.vo.XSSAndSqlUtil;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author: linghao
 * @since: 2022/7/21
 */
@Component
@Aspect
public class DemoAspject {

    @Pointcut("@within(org.springframework.web.bind.annotation.RestController)")
    public void controllerAspect() {
    }

    @Before("controllerAspect()")
    public void After(JoinPoint pjp){
        Object[] args = pjp.getArgs();
        Set<Boolean> resutleSet = new HashSet<>();
        for(Object arg : args){
            if(null != arg){
                if(arg instanceof String){  //String不需要转化，因为JSON.toJSONString(str)转化后会带双引号
                    XSSAndSqlUtil.checkXSSAndSql((String) arg,resutleSet);
                }else{
                    String s = JSON.toJSONString(args);
                    XSSAndSqlUtil.checkXSSAndSql(s,resutleSet);
                }
                if(resutleSet.contains(Boolean.TRUE)){
                    System.out.println("非法字符");
                    return;
                }
            }
        }


    }

}
